package com.study.spmongodb.wwj.thread.char05;

import java.util.Optional;
import java.util.stream.IntStream;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/6 10:01
 */
public class ThreadJoin {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            IntStream.range(1, 1000)
                    .forEach(i -> System.out.println(Thread.currentThread().getName() + " " + i));
        });
        Thread t2 = new Thread(() -> {
            IntStream.range(1, 1000)
                    .forEach(i -> System.out.println(Thread.currentThread().getName() + " " + i));
        });
        t1.start();
        t2.start();

        //等待 t1 全部执行完才执行 main
        t1.join();
        t2.join();

        //main线程会等待 上述两个线程执行结束才执行，但是 t1 和t2 有交互
        Optional.ofNullable("All of tasks finish done").ifPresent(System.out::println);
        IntStream.range(1, 1000)
                .forEach(i -> System.out.println(Thread.currentThread().getName() + " " + i));

    }

}
